local skill = fk.CreateSkill{
  name = "tea__lianhe",
}
---@type ActiveSkillSpec
local lianhe = {
  anim_type = "special",
  target_num = 1,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      local players = Fk:currentRoom().alive_players
      players = table.filter(players, function(p)
        return (table.every(players, function(p1)
          return p.hp >= p1.hp
        end) or table.every(players, function(p2)
          return #p:getCardIds("h") >= #p2:getCardIds("h")
        end)) and not p:isKongcheng()
      end)
      if #players > 0 then
        if #player:getTableMark("tea__lianhe_choose") == 0 then
          return table.contains(players, to_select)
        else
          local mark = Fk:currentRoom():getPlayerById(player:getTableMark("tea__lianhe_choose")[1]) ---@type Player
          if mark.dead then
            return table.contains(players, to_select)
          end
          local to = to_select
          if mark.dead then return false end
          return table.contains(players, to_select) and (mark:getNextAlive() == to or mark:getLastAlive() == to)
        end
      end
    end
  end,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p)
      return player:canUseTo(Fk:cloneCard("duel"), p) and not table.contains(player:getTableMark("tea__lianhe_record-turn"), p.id)
    end)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]

    local expand_pile = target:getCardIds("h")
    for _, id in ipairs(expand_pile) do
      room:setCardMark(Fk:getCardById(id), "tea__lianhe_visable-phase", 1)
    end
    local suc, dat = room:askToUseActiveSkill(player, {
      skill_name =  "tea__lianhe_viewas",
      prompt = "#tea__lianhe-invoke",
      cancelable =  true,
      extra_data = {
        is_self = player == target,
        expand_pile = player == target and {} or expand_pile,
      },
    })
    for _, id in ipairs(expand_pile) do
      room:setCardMark(Fk:getCardById(id), "tea__lianhe_visable-phase", 0)
    end
    if suc and dat and #dat.cards == 1 then
      local cards = table.clone(dat.cards)
      if not table.contains(target:getCardIds("h"), cards[1]) and player ~= target then
        cards = table.random(target:getCardIds("h"), 1)
      end
      local tos = dat.targets
      local c = Fk:cloneCard("duel")
      c:addSubcards(cards)
      c.skillName = skill.name
      local use = {
        from = player,
        tos = tos,
        card = c,
      }
      room:setPlayerMark(player, "@tea__lianhe_choose", {})
      room:setPlayerMark(player, "tea__lianhe_choose", {})
      local mark = player:getTableMark("tea__lianhe_record-turn")
      table.insertTableIfNeed(mark, table.map(tos, Util.IdMapper))
      room:setPlayerMark(player, "tea__lianhe_record-turn", mark)
      room:useCard(use)
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local lianhe_delay = {
  can_refresh = function(self, event, target, player, data)
    return table.contains((data.card or {}).skillNames or {}, skill.name) and player:hasSkill(self, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      if data.card:isVirtual() and #data.card.subcards == 1 then
        local cids = data.card.subcards
        local c = Fk:getCardById(cids[1])
        if c.trueName == "slash" and (room:getCardArea(c.id) == Card.DiscardPile or room:getCardArea(c.id) == Card.Processing) then
          room:obtainCard(player, c, true, fk.ReasonPrey)
        end
      end
    else
      if #player:getTableMark("tea__lianhe_choose") == 0 then
        room:setPlayerMark(player, "@tea__lianhe_choose", {target.general})
        room:setPlayerMark(player, "tea__lianhe_choose", {target.id})
      else
        local mark = player:getTableMark("tea__lianhe_choose")
        local shownmark = player:getTableMark("@tea__lianhe_choose")
        table.insertIfNeed(mark, target.id)
        table.insertIfNeed(shownmark, target.general)
        room:setPlayerMark(player, "tea__lianhe_choose", mark)
        room:setPlayerMark(player, "@tea__lianhe_choose", shownmark)
      end
    end
  end,
}
---@type ProhibitSpec
local lianhe_prohibit = {
  is_prohibited = function(self, from, to, card)
    return (from:hasSkill(self, true) and table.contains(from:getTableMark("tea__lianhe_record-turn"), to.id) and table.contains(card.skillNames, lianhe.name))
     or (table.contains(card.skillNames, "tea__lianhe_viewas") and table.contains(from:getTableMark("tea__lianhe_record-turn"), to.id))
  end,
}
---@type VisibilitySpec
local lianhe_visable = {
  card_visible = function(self, player, card)
    if player:hasSkill("tea__lianhe", true) and card:getMark("tea__lianhe_visable-phase") > 0 then
      return false
    end
  end
}
skill:addEffect("active", lianhe)
skill:addEffect(fk.CardUseFinished, lianhe_delay)
skill:addEffect(fk.Damaged, lianhe_delay)
skill:addEffect("prohibit", lianhe_prohibit)
skill:addEffect("visibility", lianhe_visable)
return skill